package com.kogi.androidpatt.dealing.options;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;

import com.kogi.patterns.R;


import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.ContextWrapper;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class ChangeView extends Activity{
	
	private boolean click=true;
	ViewStub stub,stub2;
	int start=0,start2=0;
	private Menu custMenu;
	private int opc1,opc2;
	private ImageAdapterDealingData iada;
	private EfficientAdapter ListDealigData;
	private MenuItem Mopc1;
	ArrayList<HashMap<String, String>> list;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.change_view_options);
        
           
        stub = (ViewStub) findViewById(R.id.stub1);
        stub2 = (ViewStub) findViewById(R.id.stub2);
    
        
        custMenu = (Menu) findViewById(R.menu.menu);
       // Mopc1 = (MenuItem) findViewById("@+id/viewB");
        
        stub.inflate();
        stub2.inflate();
        stub2.setVisibility(View.GONE);
        
      
        Context a=this;
         iada =new ImageAdapterDealingData(a);
         
         
         ListDealigData = new EfficientAdapter(a,iada);
         ListView l1 = (ListView) findViewById(R.id.listViewdealingDataList);
         l1.setAdapter(ListDealigData);
        
        
        GridView gridview = (GridView) findViewById(R.id.gridViewdealingDataicons);
       
       // gridview.setAdapter(new ImageAdapterDealing(this,a,null,ac));
       gridview.setAdapter(iada);
       
       //stub.setVisibility(View.GONE);
        
        //cargar mediante archivo
       /* */
	       
	      
	}
	
 
	
 public void cambioMenu(int pos){
		 
		 if(click==true){
			
			    men.removeItem(554);
			    men.removeItem(555);
			    men.add(0, 554, 0, R.string.Icons);
			    men.add(0, 555, 0, R.string.About);

			// custMenu.getItem(pos).setTitle("Select as List");
			 stub.setVisibility(View.GONE);
			 stub2.setVisibility(View.VISIBLE);	
			 click=false;
		 }
		 else{

			 men.removeItem(554);
			 men.removeItem(555);
			 men.add(0, 554, 0, R.string.List);
			 men.add(0, 555, 0, R.string.About);
			 
			 stub2.setVisibility(View.GONE);						
			 stub.setVisibility(View.VISIBLE);
			 click=true;
		 }
		 
	 }
	 
	 @Override
	    public boolean onOptionsItemSelected(MenuItem item) {
	        // Handle item selection
	        switch (item.getItemId()) {
	        case 554:
	        	cambioMenu(R.id.viewB);
	            return true;
	        case R.id.about:
	            //metodo
	            return true;
	        default:
	            return super.onOptionsItemSelected(item);
	        }
	    }
	 
	 private Menu men;
	 
	 @Override
	    public boolean onCreateOptionsMenu(Menu menu) {
	       // MenuInflater inflater = getMenuInflater();
	        //MenuInflater inflater2 = getMenuInflater();
	       // inflater.inflate(R.menu.menu_seg, menu);
		    men=menu;
	        menu.add(0, 554, 0, R.string.List);
	        menu.add(0, 555, 0, R.string.About);
	        
	       // inflater2.inflate(R.menu.menu_seg, menu);
	        return true;
	    }
//implemento adaptador grid----------------------------------------------	 
	 public class ImageAdapterDealingData extends BaseAdapter
	   {
	      Context MyContext;
	      ArrayList<String> texts;
	      ArrayList<Integer> images;
	      int items=0;
	      int tamfix=0;
	      
	      public ImageAdapterDealingData(Context _MyContext)
	      {
	         MyContext = _MyContext;
	         images = new ArrayList<Integer>();
	         texts = new ArrayList<String>();
	         readFile();
	         
	      }
	      
	      @Override
	      public int getCount()
	      {
	                       
	         return images.size();
	      }

	      @Override
	      public View getView(int position, View convertView, ViewGroup parent)
	      {
	    	  View grid;
	    	    
	    	   if(convertView==null){
	    	    grid = new View(MyContext);
	    	    LayoutInflater inflater=getLayoutInflater();
	    	    grid=inflater.inflate(R.layout.grid_item, parent, false);
	    	   }
	    	   else{
	    	    grid = (View)convertView;
	    	   }
	    	   
	    	   ImageView imageView = (ImageView)grid.findViewById(R.id.grid_item_image);
	    	   TextView textView = (TextView)grid.findViewById(R.id.grid_item_text);
	    	   imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
	    	   imageView.setImageResource(images.get(position));
	    	   textView.setText(texts.get(position));

	    	   return grid;
	      }

	      @Override
	      public Object getItem(int arg0) {
	         // TODO Auto-generated method stub
	         return images.get(arg0);
	      }

	      @Override
	      public long getItemId(int arg0) {
	         // TODO Auto-generated method stub
	         return arg0;
	      }
	      
	      public ArrayList<String> getNames(){
	    	  return texts;
	      }
	      
	      public void readFile(){
	    	  InputStream in;
	  	  	
	      	ContextWrapper Fcont=(ContextWrapper) MyContext;
	  		in= Fcont.getResources().openRawResource(R.raw.coutries_flags);
	      	
	      	
	  		try {
	  			
	  			 	//StringBuffer inLine = new StringBuffer();
	      	        InputStreamReader isr = new InputStreamReader(in);
	      	 
	      	        BufferedReader inRd = new BufferedReader(isr);
	      	      String text;
	      	      
	      	       	      
	      	          
	      	        while ((text = inRd.readLine()) != null) {
	      	           
	      	         String line[]= text.split(";"); 
	      	        String mDrawableName = line[1];
	      	        Integer tmp=getResources().getIdentifier(mDrawableName , "drawable", getPackageName());
	      	        images.add(tmp);
	      	        texts.add(line[0]); 
	      	         
	      	        Log.d(line[0], line[1]);
	      	        
	      	         items++;
	      	            
	      	        }
	      	        in.close();
	      	        
	  		} catch (FileNotFoundException e) {
	  			// TODO Auto-generated catch block
	  			e.printStackTrace();
	  		} catch (IOException e) {
	  			// TODO Auto-generated catch block
	  			e.printStackTrace();
	  		}
	      }

		
	   }
	 
	 public class EfficientAdapter extends BaseAdapter  {

			private Context MyContext;
			private ImageAdapterDealingData data;
			int tam;
			ArrayList<String> Names; 
			
			public EfficientAdapter(Context a, ImageAdapterDealingData info) {
				MyContext=a;
				data = info;
				//Names = new ArrayList<String>();
				Names = data.getNames();
				tam= Names.size();
			}

			@Override
			public int getCount() {
				// TODO Auto-generated method stub
				return tam;
			}

			@Override
			public Object getItem(int arg0) {
				// TODO Auto-generated method stub
				return Names.get(arg0);
			}

			@Override
			public long getItemId(int arg0) {
				// TODO Auto-generated method stub
				return arg0;
			}

			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				View grid;
			    
		 	   if(convertView==null){
		 	    grid = new View(MyContext);
		 	    LayoutInflater inflater=getLayoutInflater();
		 	    grid=inflater.inflate(R.layout.custom_row_list_view, parent, false);
		 	   }
		 	   else{
		 	    grid = (View)convertView;
		 	   }
		 	   
		 	   //ImageView imageView = (ImageView)grid.findViewById(R.id.);
		 	   TextView textView = (TextView)grid.findViewById(R.id.textdealingDataListView);
		 	   //imageView.setImageResource(images.get(position));
		 	   textView.setText(Names.get(position));

		 	   return grid;
			}

			

		}

}
